{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 配对设计资料的符号秩和检验\n",
    "\n",
    "## 案例\n",
    "\n",
    "研究在指导患有轻度牙周疾病的成年人进行良好的口腔保健的 6 个月后，牙周情况是否有好转。\n",
    "\n",
    "### 数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (28, 2)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>ID</th><th>score</th></tr><tr><td>i64</td><td>i64</td></tr></thead><tbody><tr><td>1</td><td>3</td></tr><tr><td>2</td><td>3</td></tr><tr><td>3</td><td>3</td></tr><tr><td>4</td><td>3</td></tr><tr><td>5</td><td>2</td></tr><tr><td>&hellip;</td><td>&hellip;</td></tr><tr><td>24</td><td>-1</td></tr><tr><td>25</td><td>-2</td></tr><tr><td>26</td><td>-2</td></tr><tr><td>27</td><td>-3</td></tr><tr><td>28</td><td>-3</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (28, 2)\n",
       "┌─────┬───────┐\n",
       "│ ID  ┆ score │\n",
       "│ --- ┆ ---   │\n",
       "│ i64 ┆ i64   │\n",
       "╞═════╪═══════╡\n",
       "│ 1   ┆ 3     │\n",
       "│ 2   ┆ 3     │\n",
       "│ 3   ┆ 3     │\n",
       "│ 4   ┆ 3     │\n",
       "│ 5   ┆ 2     │\n",
       "│ …   ┆ …     │\n",
       "│ 24  ┆ -1    │\n",
       "│ 25  ┆ -2    │\n",
       "│ 26  ┆ -2    │\n",
       "│ 27  ┆ -3    │\n",
       "│ 28  ┆ -3    │\n",
       "└─────┴───────┘"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import polars as pl\n",
    "\n",
    "lf = pl.read_csv(\"B_10_2-data.csv\").lazy()\n",
    "\n",
    "lf.collect()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 分析\n",
    "\n",
    "结果变量为多项有序分类变量，影响变量为二项分类变量，且两个整体的数据之间可以两两配对。可采用配对设计资料的符号秩和检验。\n",
    "\n",
    "### 检验\n",
    "\n",
    "#### 假设\n",
    "\n",
    "$ H_0 $: 在学习口腔卫生保健后，牙周情况变化的总体中位数为零。  \n",
    "$ H_1 $: 在学习口腔卫生保健后，牙周情况变化的总体中位数为零。\n",
    "\n",
    "#### 假设检验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "T = 91.0\n",
      "P = 0.15108630570822223\n",
      "significance: False\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from scipy.stats import wilcoxon\n",
    "\n",
    "res = wilcoxon(lf.select(\"score\").collect(), correction=True, method=\"auto\", zero_method=\"wilcox\")\n",
    "\n",
    "print(\n",
    "f\"\"\"T = {float(res.statistic.sum())}\n",
    "P = {float(res.pvalue.sum())}\n",
    "significance: {float(res.pvalue.sum()) < 0.05}\n",
    "\"\"\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$ P \\geqslant 0.05 $，按 $ \\alpha = 0.05 $ 水准，不应拒绝 $ H_0 $，认为在学习口腔卫生保健后，牙周情况变化的总体中位数为零。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
